package com.jay.lang.clazz;

import java.io.Serializable;
import java.util.Arrays;

public class ObjectCls implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4439500172314965247L;

	private int value;
	private byte[] data;

	// Defense copy
	public ObjectCls(int value, byte[] data) {
		this.value = value;
		this.data = Arrays.copyOf(data, data.length);
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(byte[] data) {
		this.data = data;
	}

	@Override
	public int hashCode() {
		int h = 0;
		h = 31 * h + value;
		if (null != this.data) {
			for (int i = 0; i < this.data.length; i++) {
				h = 31 * h + data[i++];
			}
		}
		return h;
	}

	// 看业务意义
	@Override
	public boolean equals(Object o) {
		if (null == o || !(o instanceof ObjectCls)) {
			return false;
		}
		ObjectCls k = (ObjectCls) o;
		if (this.value != k.getValue()) {
			return false;
		}
		if (null == k.getData() && null == this.getData()) {
			return true;
		}
		if (null == k.getData() || null == this.getData()) {
			return false;
		}
		if (k.getData().length != this.getData().length) {
			return false;
		}
		for (int i = 0; i < this.data.length; ++i) {
			if (this.data[i] != k.getData()[i]) {
				return false;
			}
		}
		return true;
	}

	// 看具体业务是否需要deep copy
	// 一般不需要此方法
	@Override
	protected Object clone() {
		return new ObjectCls(value, data);
	}

	@Override
	public
	String toString(){
		return this.value + "message";
	}
}
